﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.DesignPattern.Observer
{
    public class Subject : ISubject
    {
        private List<IObserver> _lstObserver;

        public Subject()
        {
            _lstObserver = new List<IObserver>();
        }

        #region ISubject Members

        public void Attach(IObserver observer)
        {
            if (_lstObserver.IndexOf(observer) < 0)
                _lstObserver.Add(observer);
        }

        public void Detach(IObserver observer)
        {
            if (_lstObserver.IndexOf(observer) < 0)
                return;

            _lstObserver.Remove(observer);
        }

        public void Notify(IMessageInfo miMessage)
        {
            foreach (IObserver observer in _lstObserver)
            {
                observer.Update(this, miMessage);
            }
        }

        #endregion
    }
}
